﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class SkillDurableLogic : SkillLogicBase
{

    private GameObject _particle;
    protected override void InitTimeLine()
    {
        _timeLine.AddEvent(0,0,onSkillStart);
        _timeLine.AddEvent(0, 30, onAction);
        _timeLine.AddEvent(0, 4, onParticle);
        _timeLine.AddEvent(0, 1, onDurableObject);//伤害结算
        _timeLine.AddEvent(1.33f, 30, onActionEnd);
        _timeLine.AddEvent(1.133f, 0, onSkillEnd);
    }

    private void onDurableObject(int durableID)
    {
        var tableData = SettlementTable.GetInstance()[durableID];
        if (tableData == null) { return; }
        var durableObj = ResManager.GetInstance().load<GameObject>(tableData.ResPath, _caster.transform.Find("LookPos").position);
        var Durable=durableObj.AddComponent<DurableObject>();
        Durable.Init(_caster,TableData.DuringTime,0.1f,tableData.Radius,onHitSomething);
    }

    private void onHitSomething(DurableObject durableObj, List<Creature> hitList)
    {
        //ResManager.GetInstance().Release(durableObj.gameObject);
        foreach(var creature in hitList)
        {
            //Debug.Log(creature.gameObject.name);
            DamageMgr.GetInstance().Damage(_caster, creature);
        }
    }

    private void onParticle(int particleID)
    {
        var paiticleTable=ParticleTable.GetInstance()[particleID];
        if (paiticleTable == null) { return; }
        _particle=ResManager.GetInstance().load<GameObject>(paiticleTable.ResPath,_caster.transform.Find("LookPos").position);
        _particle.transform.SetParent(_caster.transform,false);
    }
    
    protected override void onSkillEnd(int __null)
    {
        base.onSkillEnd(__null);
        ResManager.GetInstance().Release(_particle);
        
    }
}
